var util = require("../../../utils/util.js"),
  api = require("../../../utils/api.js"),
  app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    popShow: {
      type: Boolean,
      value: false,
      observer(newVal) {
        this.setData({show:newVal})
        if (newVal == false) {
          this.setData({
            userXyVal:false,
            userHtVal:false,
            userCwVal:false,
            userXxfVal:false,
            userGszcVal:false
          })
        }
      }
    },
    isMatchmaker: {
      type: Number,
      value: 0,
      observer(newVal) {
        this.setData({isMatchmakers:newVal})
      }
    },
    suerMobile: {
      type: String,
      value: '',
      observer(newVal) {
        this.setData({
          mobile: newVal
        })
      }
    },
    suerNickname: {
      type: String,
      value: '',
      observer(newVal) {
        this.setData({
          nickname: newVal
        })
      }
    },
    popHeight: {
      type: String,
      value: '50%',
      observer(newVal) {
        this.setData({
          height: newVal
        })
      }
    },
    popBrankNum: {
        type: String,
        value: '',
        observer(newVal) {
          this.setData({
            brank_num: newVal 
          })
        }
      },
      popBranch: {
        type: String,
        value: '',
        observer(newVal) {
          this.setData({
            branch: newVal
          })
        }
      },
  },

  /**
   * 组件的初始数据
   */
  data: {
    userXyVal:false,
    userHtVal:false,
    userCwVal:false,
    userXxfVal:false,
    userGszcVal:false,
    isMatchmakers:0,
    nickname: '',
    show: false,
    mobile: '',
    brank_num:'',
    branch:'',
    height:'50%'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onClose() {
      this.triggerEvent('bindClose', {res:false})
    },
    getNewsContent(e) {
      let index = e.currentTarget.dataset.index;
      this.setData({
        newshow: true,
        newContent: this.data.newsList[index].content
      })
    },
    getUserName(e) {
      this.setData({
        nickname: e.detail
      })
    },
    getMobile(e) {
      this.setData({
        mobile: e.detail
      })
    },
    getInputChange(e) {
        let type = e.target.dataset.type;
        this.data[type] = e.detail;
        this.setData({
          type
        })
      },
    getPhoneNum: function (e) {
      console.log(e);
      if (e.detail.errMsg == "getPhoneNumber:ok") {
        var a = {
          sessionKey: wx.getStorageSync('session_key'),
          iv: e.detail.iv,
          encryptedData: e.detail.encryptedData
        };
        this.parsePhoneNumber(a);
      } else {
        wx.showModal({
          title: '提示',
          content: '您的手机号码获取出现了错误',
          cancelText: '知道了',
          showCancel: false
        })
      }
    },
    parsePhoneNumber: function (e) {
      api.default.getWxTel(e).then(res => {
        if (res.data) {
          let josn = JSON.parse(res.data);
          this.setData({
            mobile: josn.phoneNumber
          })
        }
      })
    },
    userxy(e) {
      this.setData({
        userXyVal: e.detail 
      })
    },
    hetong(e) {
      this.setData({
        userHtVal: e.detail
      })
    },
    cwtong(e) {
      this.setData({
        userCwVal: e.detail
      })
    },
    xxftong(e) {
      this.setData({ 
        userXxfVal: e.detail 
      }) 
    },
    gszctong(e) {
      this.setData({
        userGszcVal: e.detail
      })
    },
    onSubmit() {
      const {
        nickname,
        mobile,
        userXyVal,
        userHtVal,
        userCwVal,
        brank_num,
        branch
      } = this.data;
      if (!/^1[2|3|4|5|7|8|9]\d{9}$/.test(mobile)) {
        return wx.showToast({
          title: '手机号码有误',
          icon: 'none'
        })
      }
      if (!nickname || nickname == '') {
        return wx.showToast({
          title: '请填写姓名',
          icon: 'none'
        })
      }
      if (userXyVal == false) {
        return wx.showToast({
          title: '请阅读并同意用户协议',
          icon: 'none'
        })
      }
      if (this.data.isMatchmakers>0) {
        if (!/^[1-9]\d{15,18}$/.test(brank_num)) {
            return wx.showToast({
              title: '银行卡号有误',
              icon: 'none'
            })
        }
        if (!branch || branch == '') {
            return wx.showToast({
              title: '请填写开户行',
              icon: 'none'
            })
          }
        if (userHtVal == false) {
          return wx.showToast({
            title: '请阅读并同意用户合同',
            icon: 'none'
          })
        }
        if (userCwVal == false) {
          return wx.showToast({
            title: '请阅读并同意财务代管合同',
            icon: 'none'
          })
        }
        if(this.data.isMatchmakers>1){ 
          if (this.data.userXxfVal == false) {
            return wx.showToast({
              title: '请阅读并同意信息服务协议',
              icon: 'none'
            })
          }
          if (this.data.userXxfVal == false) {
            return wx.showToast({
              title: '请阅读并同意工商注册流程模板',
              icon: 'none'
            })
          }
        }
      } 
      
      this.triggerEvent('bindSubmit', {nickname,mobile,brank_num,branch})
      this.triggerEvent('bindClose', {res:false})
    },
    readyPdf(e) {
      const {
        flag
      } = e.currentTarget.dataset;
      api.default.config().then((a) => {
        if (a.code == 1) {
          let url = '';
          if (flag == 1) {
            url = app.globalData.baseImgUrl + a.data.agreement;
          } else if (flag == 2) {
            url = app.globalData.baseImgUrl + a.data.contract;
          } else if (flag == 3) {
            url = app.globalData.baseImgUrl + a.data.financial_contract;
          }else if (flag == 4) {
            url = app.globalData.baseImgUrl + a.data.information_service_agreement;
          }else if (flag == 5) {
            url = app.globalData.baseImgUrl + a.data.business_registration_process;
          }
          console.log('url',url)
          wx.downloadFile({
            url: url,
            success: function (resinfo) {
              console.log("pdf协议文件已下载")
              let path = resinfo.tempFilePath;
              console.log(path, resinfo)
              wx.openDocument({
                filePath: path,
                fileType: 'pdf',
                success: function (rest) {
                  console.log(rest);
                },
                fail: function (error) {
                  console.log('error',error)
                  wx.showToast({
                    icon: 'none',
                    title: '打开文件失败'
                  });
                },
              })
            },
            fail: function (err) {
              wx.showToast({
                icon: 'none',
                title: '下载文件失败'
              });
            }
          })
        }
      });
    },
    cwtong(e) {
      this.setData({
        userCwVal: e.detail
      })
    },
  }
})